package com.woniuxy.store.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CommonResult;
import com.woniuxy.store.core.model.MdStore;
import com.woniuxy.store.core.vo.MdStoreVo;
import com.woniuxy.store.core.service.MdStoreService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/mdStore")
public class MdStoreController {
    @Autowired(required = false)
    MdStoreService storeService;

    //查新所有门店列表
    @GetMapping("/selectAllstores")
    @ApiOperation("查询所有门店列表")
    public CommonResult selectAllstores(){
        List<MdStore> storelist=this.storeService.list();
        storelist.toString();
        return CommonResult.success(storelist);
    }

    //查询一个门店的所有菜单信息
    @ApiOperation("查询单个门店")
    @GetMapping("/selectOnestore/{storeId}")
    public CommonResult selectOnestore(@PathVariable("storeId") int storeId) {
        MdStoreVo store=this.storeService.selectOnestore(storeId);

        return CommonResult.success(store);
    }

    //增加一个门店
    @PostMapping("/insertOnestore")
    public CommonResult insertOnestore(@RequestBody MdStore mdStore){
        this.storeService.insertOnestore(mdStore);
        return CommonResult.success("插入成功");
    }
    //删除一个门店
    @GetMapping("delectOnestore/{storeId}")
    public CommonResult delectOnestore(@PathVariable("storeId") int storeId) {
        this.storeService.delectOnestore(storeId);
        return CommonResult.success("注销成功");
    }

    //修改门店信息
    @PostMapping("updateOnestore")
    public CommonResult updateOnestore(@RequestBody MdStore mdStore){
      boolean b=  this.storeService.updateById(mdStore);
      if(b){
        return CommonResult.success("更新成功");}
      else {
          return CommonResult.fail();
      }
    }

    /**
     * Description : 商家入驻平台申请  <br/>
     * ChangeLog : 1. 创建 (2021/7/6 11:33 [binhu]);
     * @param mdStore
     * @return com.woniuxy.common.CommonResult
    */
    @PostMapping("/applicationForAdmission")
    public  CommonResult applicationForAdmission(@RequestBody MdStore mdStore){
        String storeName = mdStore.getStoreName();
        MdStore store = storeService.selectByStoreName(storeName);
        if(store != null){
            return new CommonResult(300,"已申请加盟");
        }
        int row = storeService.application(mdStore);
        if(row == 0){
            return CommonResult.fail("申请失败，请重试");
        }
        return CommonResult.success("申请中");
    }

    /**
     * Description : 审核  <br/>
     * ChangeLog : 1. 创建 (2021/7/6 12:10 [binhu]);
     * @param storeId
     * @return com.woniuxy.common.CommonResult
    */
    @PostMapping("/check/{storeId}")
    public  CommonResult check(@PathVariable Integer storeId){
        int row = storeService.check(storeId);
        if(row == 0){
            return CommonResult.fail("审核失败");
        }
        return CommonResult.success("审核成功");
    }

    /**
     * Description : 分页  <br/>
     * ChangeLog : 1. 创建 (2021/7/9 15:35 [binhu]);
     * @param startpage
     * @param pagesize
     * @return com.woniuxy.common.CommonResult
    */
    @GetMapping("/selectAll")
    public CommonResult selectAll(int startpage, int pagesize,String storeName) {
        PageHelper.startPage(startpage,pagesize);
        Page<MdStore> page = (Page<MdStore>) this.storeService.queryAllByLimit(storeName);
        PageInfo<MdStore> pageInfo = new PageInfo<>(page);

        return CommonResult.success(pageInfo);
    }


    /**
     * Description : 上传  <br/>
     * ChangeLog : 1. 创建 (2021/7/9 15:36 [binhu]);
     * @param file
     * @return com.woniuxy.common.CommonResult
    */
    @PostMapping("/upload")
    public CommonResult upload(MultipartFile file){
        String path = storeService.upload(file);
        return CommonResult.success(path);
    }


}

